var login_id;
let select_cart =[];
let default_address ;
let pay_code = "alipay";
let pay_name = "支付宝";
$(document).ready(function () {
    var login = isLogin();
    if (login == undefined) {
        alert("去登陆")
        location.href = "login.html";
    }
    $.ajax({
        type: "get",
        url: "http://localhost:3301/user?phone=" + login,
        async: false,
        success: function (e) {
            login_id = e[0].id;
        }
    })
    if (login_id == undefined) {
        alert("去登陆")
        location.href = "login.html";
    }
    $(".newadd").click(function () {
        $("#myModalLabel").html("添加收货地址");
        show_box();
    })
    $(".sui-close").click(function () {
        close_box();
    })
    $(".sui-modal-backdrop").click(function () {
        close_box();
    })
    $(".modal-footer .btn-default").click(function () {
        close_box();
    })
    //加载地址信息
    load_address();
    $(".addressInfo .address").hover(function () {
        $(this).addClass("address-hover");
    }, function () {
        $(this).removeClass("address-hover");
    })
    $(".addressInfo .edit_address").click(function () {
        $("#myModalLabel").html("编辑收货地址");
        show_box();
        let addr = get_address($(this).attr("data-id"));
        $(".form-horizontal [name=id]").val(addr.id);
        $(".form-horizontal [name=consignee]").val(addr.consignee);
        $(".form-horizontal [name=province]").val(addr.province);
        cityShow();
        $(".form-horizontal [name=city]").val(addr.city);
        district();
        $(".form-horizontal [name=district]").val(addr.district);
        let adr = $(".form-horizontal [name=address]").val(addr.address);
        let phone = $(".form-horizontal [name=phone]").val(addr.phone);
        $(".form-horizontal [name=is_default]").prop("checked", addr.is_default == "0" ? false : true);
    })
    $(".addressInfo .base").click(function () {
        put_address_is_selected($(this).attr("data-id"));
        load_address();
    })
    $(".addressInfo .delete_address").click(function () {
        delete_address($(this).attr("data-id"));
        load_address();
    })
    $(".addr-item").click(function(){
        $(".addr-item").find(".name").removeClass("selected");
        let rid = $(this).attr("data-id");
        $(this).find(".name").addClass("selected");
        default_address = get_address(rid);
        compute();
    })
    //获取商品信息
    if (login_id != undefined) {
        select_cart = get_selected_cart(login_id);
        console.log(select_cart);
        if ($.isEmptyObject(select_cart[0])) {
            alert("请选中要结算的商品")
            location.href = "cart.html";
        }
        select_cart.forEach((e, i) => {
            $(".sendGoods").append($(`
            <ul class="yui3-g">
                <li class="yui3-u-1-6">
                    <span><img style="height:100px;" src="${e.goods_logo}"></span>
                </li>
                <li class="yui3-u-7-12">
                    <div class="desc">${e.goods_name}<br>${e.value_names}</div>
                    <div class="seven">7天无理由退货</div>
                </li>
                <li class="yui3-u-1-12">
                    <div class="price">￥${e.price}</div>
                </li>
                <li class="yui3-u-1-12">
                    <div class="num">X${e.number}</div>
                </li>
                <li class="yui3-u-1-12">
                    <div class="exit">有货</div>
                </li>
            </ul>`
            ));
        })

    } else {
        alert("去登陆")
        location.href = "login.html";
    }
    compute();
    $(".payType li").click(function(){
        $(".payType li").removeClass("selected");
        pay_code = $(this).attr("pay_type");
        pay_name = $(this).text().trim();
        $(this).addClass("selected");
    })
    $(".submit a").click(function(){
        if(default_address==undefined){
            alert("请选择地址");
        }
        console.log(select_cart);
        let order = {
            "order_sn": new Date().getTime()+""+parseInt(Math.random()*100000),
            "user_id": login_id,
            "order_status": 0,
            "consignee": default_address.consignee,
            "address": default_address.address+""+default_address.area,
            "phone": default_address.phone,
            "shipping_code": "",
            "shipping_name": "",
            "shipping_sn": "",
            "pay_code": pay_code,
            "pay_name": pay_name,
            "invoice_title": "",
            "taxpayer": "",
            "invoice_desc": null,
            "user_note": "",
            "goods_price": 0,
            "shipping_price": 0,
            "coupon_price": 0,
            "order_amount": 200000,
            "total_amount": select_cart.total_price,
            "create_time": new Date().getTime(),
            "shipping_time": 0
        };
        post_order(order,select_cart);
    })
})
function load_address() {
    //获取地址
    $.ajax({
        type: "get",
        url: `http://localhost:3301/address?user_id=${login_id}&_sort=is_default&_order=desc`,
        async: false,
        success: function (e) {
            let adr = $(".addressInfo .addr-detail");
            adr.empty();
            e.forEach((e, i) => {
                if(e.is_default == '1'){
                    default_address = e;
                }
                adr.append(`<li class="addr-item" data-id="${e.id}">
                <div class="con name ${e.is_default == '0' ? "" : "selected"}"><a href="javascript:;"><em>${e.consignee}</em><span title="点击取消选择">&nbsp;</span></a></div>
                <div class="con address">
                    <span class="consignee_name">${e.consignee}</span>
                    <span class="consignee_address">${e.area} ${e.address}</span>
                    <span class="consignee_phone">${e.phone}</span>
                    <span class="base" style="${e.is_default=="0"?"":"background:#c81623;"}" data-id="${e.id}">默认地址</span>
                    <span class="edittext">
                        <a class="edit_address" data-id="${e.id}" data-toggle="modal" data-target=".edit" data-keyboard="false">编辑</a>&nbsp;&nbsp;
                        <a class="delete_address" data-id="${e.id}" href="javascript:;">删除</a>
                    </span>
                </div>
                <div class="clearfix"></div>
            </li>`);
            })
        }
    })
    compute();
}

function compute(){
    $(".order-summary .number").html(select_cart.count);
    $(".order-summary .allprice").html("￥"+select_cart.total_price);
    $(".price").html("￥"+select_cart.total_price);
    if(default_address!=undefined){
        $(".fc-receiverInfo").html(`寄送至:${default_address.area} ${default_address.address} 收货人:${default_address.consignee} ${default_address.phone.substr(0,3)}****${default_address.phone.substr(-4)}`);
    }else{
        $(".fc-receiverInfo").html("还没选择地址");
    }
}